package com.wyj.wuliwuli.controller;

import com.alibaba.fastjson.JSONObject;
import com.wyj.wuliwuli.common.AjaxRequest;
import com.wyj.wuliwuli.common.AjaxResult;
import com.wyj.wuliwuli.controller.request.LoginRequest;
import com.wyj.wuliwuli.exception.TokenInvalidException;
import com.wyj.wuliwuli.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author wyj
 * @Date 2022.2.14 21:29
 * @Version 1.0
 * @Description
 */

@Api(tags = "登录")
@RestController
@RequiredArgsConstructor
public class LoginController {

    private final LoginService loginService;

    /**
     * 参数示例：
     * {
     * 	"data": {
     * 	    "username":"test",
     * 	    "password":"test"
     * 	},
     * 	"params": {}
     * }
     */
    @ApiOperation(value = "登录")
    @CrossOrigin
    @PostMapping("/api/login")
    public AjaxResult login(@RequestBody AjaxRequest<LoginRequest> ajaxRequest) throws Exception {
        return loginService.auth(ajaxRequest);
    }

    @ApiOperation(value = "刷新token")
    @PostMapping("/api/refresh-token")
    public AjaxResult refreshToken(@RequestBody AjaxRequest<JSONObject> ajaxRequest) throws Exception, TokenInvalidException {
        return loginService.refreshToken(ajaxRequest);
    }

}
